﻿using StructureMap;
using Tarantino.Core;
using Tarantino.Core.Commons.Model;
using Tarantino.Core.Commons.Services.Repositories;
using Tarantino.Samples.Core.Model;

namespace Tarantino.Samples.Core.Services.Repositories
{
	[Pluggable(ServiceKeys.Default)]
	public class SystemUserRepository : ISystemUserRepository
	{
		private readonly IPersistentObjectRepository _repository;

		public SystemUserRepository(IPersistentObjectRepository repository)
		{
			_repository = repository;
		}

		public bool IsValidLogin(string emailAddress, string password)
		{
			CriterionSet criterionSet = new CriterionSet();
			criterionSet.AddCriterion(new Criterion(SystemUser.EMAIL_ADDRESS, emailAddress));
			criterionSet.AddCriterion(new Criterion(SystemUser.ENCRYPTED_PASSWORD, password));

			SystemUser matchingUser = _repository.FindFirst<SystemUser>(criterionSet);

			bool isValidLogin = matchingUser != null;
			return isValidLogin;
		}

		public ISystemUser GetByEmailAddress(string emailAddress)
		{
			CriterionSet criterionSet = new CriterionSet();
			criterionSet.AddCriterion(new Criterion(SystemUser.EMAIL_ADDRESS, emailAddress));

			SystemUser matchingUser = _repository.FindFirst<SystemUser>(criterionSet);

			return matchingUser;
		}
	}
}